﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;

namespace Kiiro.Model.HttpHandler
{
    public class KiiroFileUploadHandler : IHttpHandler
    {
        /*  ADD TO WEB CONFIG:
    
            <httpHandlers>
                <add verb="*" path="*.fileUpload" type="Kiiro.Model.HttpHandler.KiiroFileUploadHandler, Kiiro.Model, Version=1.2.0.7, Culture=neutral, PublicKeyToken=5b2f4277f61b7816" />
            </httpHandlers>
   
            <system.webServer>
                <handlers>
                    <add verb="*" path="*.fileUpload" name="KiiroFileUploadHandler" modules="IsapiModule"
                        scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"
                        type="Kiiro.Model.HttpHandler.KiiroFileUploadHandler, Kiiro.Model, Version=1.2.0.7, Culture=neutral, PublicKeyToken=5b2f4277f61b7816" />
                </handlers>
            </system.webServer>
         */

        public bool IsReusable { get { return false; } }

        public KiiroFileUploadHandler ( ) { }

        public void ProcessRequest ( HttpContext context )
        {
            List<Attachment> attachments = new List<Attachment> ( );
            Attachment attachment = null;
            char [ ] splitChars = new char [ ] { '|' };

            //if ( ControlMode == PageMode.New || ControlMode == PageMode.Edit )
            //{
                // deleted
                //string [ ] deletedFileNames = txtDeletedAttachments.Text.Split ( splitChars, StringSplitOptions.RemoveEmptyEntries );
                //foreach ( string fileName in deletedFileNames )
                //{
                //    attachment = new Attachment ( );
                //    attachment.FileName = fileName;
                //    attachment.EditType = EditType.Deleted;
                //    attachments.Add ( attachment );
                //}

                // created
                for ( int i = 0; i < context.Request.Files.Count; i++ )
                {
                    HttpPostedFile file = context.Request.Files [ i ];
                    if ( file != null && file.ContentLength > 0 )
                    {
                        attachment = new Attachment ( );
                        attachment.Bytes = new byte [ file.ContentLength ];
                        attachment.FileName = file.FileName.Substring ( file.FileName.LastIndexOf ( "\\" ) + 1 );
                        file.InputStream.Read ( attachment.Bytes, 0, file.ContentLength );
                        attachment.EditType = EditType.Created;
                        attachments.Add ( attachment );
                    }
                }
            //}

            //return attachments;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/HttpHandler/KiiroFileUploadHandler.cs $
 * 
 * 8     4/07/10 2:33p Camerons
 * 
 * 7     2/24/10 5:14p Camerons
 * 
 * 6     2/23/10 11:46a Camerons
 * 
 * 5     2/16/10 10:11a Camerons
 * 
 * 4     2/09/10 12:48p Camerons
 * 
 * 3     2/08/10 5:04p Camerons
 * 
 * 2     1/25/10 1:17p Camerons
 * 
 * 1     1/18/10 9:56a Camerons
 * 
 *************************************************************************/
